import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from "../utils/auth"; //获取token身份
import { Notify } from "vant";
Vue.use(VueRouter);

//配置路由
const router = new VueRouter({
  routes: [
    //路由重定向
    {
      path: "/",
      redirect: "/home", //路由重定向
    },
    {
      path: "/home",
      component: () => import("@/pages/home/Home.vue"),
      meta: {
        title: "首页",
        showFooter: true,
        LeftText: "",
        LeftArrow: false,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/sort",
      component: () => import("@/pages/sort/Sort.vue"),
      meta: {
        title: "分类",
        showFooter: true,
        LeftText: "",
        LeftArrow: false,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/cart",
      component: () => import("@/pages/cart/Cart.vue"),
      meta: {
        title: "购物车",
        showHeader: false,
        showFooter: true,
        LeftText: "",
        LeftArrow: false,
        RightText: true,
      },
      //路由独享守卫，判断用户有没有登录，如果没有登录就跳转到login
      beforeEnter: (to, from, next) => {
        if (!getToken()) {
          Notify("请先登录");
          next("/login");
        } else {
          next();
        }
      },
    },
    {
      path: "/mine",
      component: () => import("@/pages/mine/Mine.vue"),
      meta: {
        title: "我的",
        showFooter: true,
        LeftText: "",
        LeftArrow: false,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/detail/:pid",
      component: () => import("@/pages/detail/Detail.vue"),
      meta: {
        title: "商品详情",
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/login",
      component: () => import("@/pages/login/Login.vue"),
      meta: {
        title: "登录",
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/register",
      component: () => import("@/pages/register/Register.vue"),
      meta: {
        title: "注册",
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/changeUserInfo",
      component: () => import("@/pages/mine/changeUserInfo.vue"),
      meta: {
        title: "个人中心",
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
        showHeader: true,
      },
    },
    {
      path: "/addressEdit",
      name: "/addressEdit",
      component: () => import("@/pages/address/addressEdit.vue"),
      meta: {
        title: "收货信息",
        showHeader: true,
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
      },
      //路由独享守卫，判断用户有没有登录，如果没有登录就跳转到login
      beforeEnter: (to, from, next) => {
        if (!getToken()) {
          Notify("请先登录");
          next("/login");
        } else {
          next();
        }
      },
    },
    {
      path: "/addressList",
      component: () => import("@/pages/address/addressList.vue"),
      meta: {
        title: "收货信息",
        showHeader: true,
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
        RightText: false,
      },
      //路由独享守卫，判断用户有没有登录，如果没有登录就跳转到login
      beforeEnter: (to, from, next) => {
        if (!getToken()) {
          Notify("请先登录");
          next("/login");
        } else {
          next();
        }
      },
    },
    {
      path: "/order",
      component: () => import("@/pages/order/order.vue"),
      meta: {
        title: "订单信息",
        showHeader: false,
        showFooter: true,
        LeftText: "返回",
        LeftArrow: true,
        RightText: true,
      },
      //路由独享守卫，判断用户有没有登录，如果没有登录就跳转到login
      beforeEnter: (to, from, next) => {
        if (!getToken()) {
          Notify("请先登录");
          next("/login");
        } else {
          next();
        }
      },
    },
    // 图表
    {
      path: "/echarts",
      component: () => import("@/pages/echarts/index.vue"),
      meta: {
        title: "图表",
        showHeader: true,
        showFooter: false,
        LeftText: "返回",
        LeftArrow: true,
      },
    },
  ],
});

export default router;
